<?php

namespace common\models\dynamic;

use Yii;

/**
 * This is the model class for table "{{%dynamic_video}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $dynamic_id 动态ID
 * @property string $path 视频路径
 * @property int $duration 时长
 * @property int $video_id 原始视频ID
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 */
class DynamicVideo extends \yii\db\ActiveRecord {

    use \common\traits\TimestampBehavior;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%dynamic_video}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['user_id', 'dynamic_id', 'duration', 'video_id', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['path'], 'required'],
            [['path'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'dynamic_id' => 'Dynamic ID',
            'path' => 'Path',
            // 'url' => function () {
            //     return 'http://oss.lnts.cn/' . $this->path;
            // },
            'duration' => 'Duration',
            'video_id' => 'Video ID',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'deleted_at' => 'Deleted At',
        ];
    }

    public function fields() {
        return [
            'id',
            'filename' => 'path',
            'duration',
            'video_url' => 'videoUrl',
            'cover_url' => 'coverUrl',
        ];
    }

    public function getVideoUrl() {
        return Yii::$app->params['alioss_video_host']['host'] . '/' . $this->path;
    }

    public function getCoverUrl() {
        $aliossVideoConfig = Yii::$app->params['alioss_video_host'];
        return $this->videoUrl . '?'
        . 'x-oss-process=video/snapshot,'
        . 't_' . $aliossVideoConfig['t'] . ','
        . 'w_' . $aliossVideoConfig['width'] . ','
        . 'h_' . $aliossVideoConfig['height'] . ','
        . 'f_' . $aliossVideoConfig['format'] . ','
        . 'm_' . $aliossVideoConfig['module'];
    }

}
